﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GQ.BLL.QuanLyKho;
using GQ.WinForms;
using GQ.BLL.Category;
using GQ.Utils.TreeNode;
using GQ.Controls;
using GQ.Controls.Tools;
using GQ.Utils;
namespace GQ.QuanLyKho
{
    public partial class frmHangHoa : frmBase
    {
        public Kho_HangHoa HangHoa;
        ValidateCollection _validates = new ValidateCollection();
        public frmHangHoa()
        {
            InitializeComponent();
            _validates.Add(new ValidateEmpty(txtMa, "Mã không được để trống", dxErrorProvider1));
            _validates.Add(new ValidateEmpty(txtTen, "Tên không được để trống", dxErrorProvider1));
            GridUtils.SetStyle(gridLookUpEdit1View);

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!_validates.Validate()) return;
            Get();
            HangHoa.Save();
            DialogResult = DialogResult.OK;
        }
        private void Set()
        {
            txtMa.Text = HangHoa.Ma;
            txtTen.Text = HangHoa.Ten;
            cboTinhChat.EditValue = HangHoa.NhomHangID;
            cboLoai.SetValue(HangHoa.LoaiHangHoaID);
            txtDonViTinh.Text = HangHoa.DonViTinh;
            txtThoiHanBH.Text = HangHoa.ThoiHanBaoHanh;
            txtTyLeQD.EditValue = HangHoa.TyLeChuyenDoi;
            txtDonGiaMua.EditValue = HangHoa.DonGiaMua;
            txtDonGiaBan.EditValue = HangHoa.DonGiaBan;
            txtThueSuat.EditValue = HangHoa.ThueSuat;
            txtGhiChu.Text = HangHoa.GhiChu;
            gridLookUpEdit1.EditValue = HangHoa.NhaCungCapID;
        }
        private void Get()
        {
            HangHoa.Ma = txtMa.Text;
            HangHoa.Ten = txtTen.Text;
            if (cboTinhChat.EditValue != null)
                HangHoa.NhomHangID = Convert.ToInt32(cboTinhChat.EditValue);
            else HangHoa.NhomHangID = null;
            if (cboLoai.Value != null)
            {
                if (cboLoai.Value.GetType() == typeof(CategoryType))
                {
                    CategoryType categorytype = (CategoryType)cboLoai.Value;
                    HangHoa.LoaiHangHoaID = categorytype.ID;
                }
                else
                {
                    Category category = (Category)cboLoai.Value;
                    HangHoa.LoaiHangHoaID = category.ID;
                }


            }
            else HangHoa.LoaiHangHoaID = null;
            HangHoa.DonViTinh = txtDonViTinh.Text;
            HangHoa.ThoiHanBaoHanh = txtThoiHanBH.Text;
            if (txtTyLeQD.EditValue != null)
                HangHoa.TyLeChuyenDoi = (decimal)txtTyLeQD.EditValue;
            else HangHoa.TyLeChuyenDoi = null;
            if (txtDonGiaMua.EditValue != null)
                HangHoa.DonGiaMua = Convert.ToDecimal(txtDonGiaMua.EditValue);
            else
                HangHoa.DonGiaMua = null;
            HangHoa.DonGiaBan = txtDonGiaBan.EditValue != null ? (decimal)txtDonGiaBan.EditValue : 0;
            HangHoa.ThueSuat = txtThueSuat.EditValue != null ? (decimal)txtThueSuat.EditValue : 0;
            HangHoa.GhiChu = txtGhiChu.Text;
            if (gridLookUpEdit1.EditValue != null) HangHoa.NhaCungCapID = (Guid)gridLookUpEdit1.EditValue;

        }
        private void frmHangHoa_Load(object sender, EventArgs e)
        {
            gridLookUpEdit1.Properties.DataSource = DoiTuong.LoadAllBy("CompanyID={0} and (LoaiDoiTuong=1 or LoaiDoiTuong=3)", Config.CompanyID);
            gridLookUpEdit1.Properties.ValueMember = "ID";
            gridLookUpEdit1.Properties.DisplayMember = "Ten";
            Singleton.FillCategory(cboLoai, Singleton.LoaiHang);
            Set();
        }
    }
}
